<template>
	<view >
		<video v-show="isOpenVideo" id="myVideo" :src="src" @fullscreenchange="fullscreenchange" @pause="pause" @ended="ended" @timeupdate="timeupdate"></video>
	</view>
</template>
 
<script>
	export default {
		name: "open-media",
		props: {
			src: {
				type: String,
				default(){
					return ''
				}
			},
		},
		data() {
			return {
				isOpenVideo: false,
				videoContext: null,
				play_duration:0,
				total_duration:0
			};
		},
		methods: {
			openMedia() {
				//打开视频（全屏播放）
				this.isOpenVideo = true;
				this.videoContext = uni.createVideoContext('myVideo', this);
				this.videoContext.requestFullScreen({
					direction:0
				});
				this.videoContext.play();
				this.play_duration = 0;
				this.total_duration = 0;
			},
			//退出全屏时停止播放
			fullscreenchange(e) {
				console.log(e);
				if (!e.detail.fullScreen) {
					this.videoContext.pause();
					this.isOpenVideo = false;
					let obj = {
						play_duration:this.play_duration,
						total_duration:this.total_duration
					}
					this.$emit('exitFull',obj);
				}
			},
			pause(e){
				
			},
			ended(e){
				
				
			},
			timeupdate(e){
				if(e.detail.currentTime == 0)return;
				let play_duration = Math.floor(e.detail.currentTime);
				let total_duration = Math.floor(e.detail.duration);
				console.log(play_duration,total_duration);
				this.play_duration = play_duration;
				this.total_duration = total_duration;
			},
			
		}
	}
</script>
 
<style lang="less">
	
</style>